﻿using FastCloud.Base.Attribute;
using FastCloud.Base.Config;
using FastCloud.Base.DB;
using FastCloud.Base.Entity;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Base.Filter
{
    [CloudFilter(SoryNum = 79)]
    public class RequestLogFilter : ActionFilterAttribute
    {
        private TokenUtil TokenUtil = (TokenUtil)InjectContext.Resolve(typeof(TokenUtil));
        private DB_VisitLog DB_VisitLog = (DB_VisitLog)InjectContext.Resolve(typeof(DB_VisitLog));
        private DB_OperationLog DB_OperationLog = (DB_OperationLog)InjectContext.Resolve(typeof(DB_OperationLog));

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var Request = context.HttpContext.Request;
            var ControllerEntity = context.ActionDescriptor as ControllerActionDescriptor;
            //配置文件配置的匿名访问
            bool IsConfigAnonymous = CloudUtil.CheckIsAnonymousAccess(Request.Path);
            //代码标注的匿名访问
            bool IsCodeControolerAnonymous = context.Controller.GetType().GetCustomAttributes(typeof(AllowAnonymous), true).Count() > 0;
            bool IsCodeActionAnonymous = ControllerEntity.MethodInfo.GetType().GetCustomAttributes(typeof(AllowAnonymous), true).Count() > 0;

            //配置文件配置的记录日志
            bool IsRequestLog = CloudUtil.CheckIsRequestLog(Request.Path);
            //代码标注的记录日志
            bool IsCodeControllerLog = context.Controller.GetType().GetCustomAttributes(typeof(RequestLog), true).Count() > 0;
            bool IsCodeActionLog = ControllerEntity.MethodInfo.GetCustomAttributes(typeof(RequestLog), true).Count() > 0;

            bool IsAuthController = context.Controller.GetType().GetCustomAttributes(typeof(TokenAuth), true).Count() > 0;
            bool IsAjaxCall = context.HttpContext.Request.Headers["x-requested-with"] == "XMLHttpRequest";

            if (IsRequestLog || IsCodeControllerLog || IsCodeActionLog)
            {
                if (IsAjaxCall)
                {
                    //记录数据操作日志
                    string Param = string.Empty;
                    string FormParam = string.Empty;
                    foreach (var formitem in CloudContext.Current.Request.Form)
                    {
                        StringValues ParamValues;
                        CloudContext.Current.Request.Form.TryGetValue(formitem.Key, out ParamValues);
                        if (ParamValues.Count > 0)
                        {
                            string Value = ParamValues[0];
                            FormParam += formitem.Key + ":" + Value + ",";
                        }
                    }
                    FormParam = FormParam.TrimEnd(',');
                    Param += FormParam;
                    if (IsAuthController)
                    {
                        Frame_OperationLog OperateEntity = new Frame_OperationLog();
                        OperateEntity.OperateContent = string.Format("请求Url：{0}，请求参数类型：{1}，请求参数：{2}",
                            Request.Path, Request.ContentType, Param);
                        OperateEntity.OperateType = "AjaxRequest";
                        if (IsCodeControolerAnonymous || IsCodeActionAnonymous || IsConfigAnonymous)
                        {
                            OperateEntity.OperateOUGuid = "匿名访问";
                            OperateEntity.OperateOUName = "匿名访问";
                            OperateEntity.OperateDisplayName = "匿名访问";
                            OperateEntity.OperateUserGuid = "匿名访问";
                        }
                        DB_OperationLog.Insert(OperateEntity);
                    }
                }
                else
                {
                    if (IsAuthController)
                    {
                        //记录模块访问日志
                        Frame_VisitLog VistEntity = new Frame_VisitLog();
                        VistEntity.ModuleCode = "";
                        VistEntity.ModuleGuid = "";
                        VistEntity.ModuleName = "";
                        VistEntity.ModuleUrl = Request.Path;
                        if (IsCodeControolerAnonymous || IsCodeActionAnonymous || IsConfigAnonymous)
                        {
                            VistEntity.VisitOUName = "匿名访问";
                            VistEntity.VisitOUGuid = "匿名访问";
                            VistEntity.VisitUserGuid = "匿名访问";
                            VistEntity.VisitDisplayName = "匿名访问";
                        }
                        DB_VisitLog.Insert(VistEntity);
                    }
                }
                base.OnActionExecuting(context);
            }
            else
            {
                base.OnActionExecuting(context);
            }
        }
    }
}
